/**
 * 
 */
package com.autonavi.car.si.FileStore.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * 线程池工具
 * 
 * @author kucheng.liao@autonavi.com
 * @Date Oct 10, 2014
 */
public class ThreadPoolUtil {

	private static volatile ExecutorService pool = null;

	private static void init() {
		if (null == pool) {
			synchronized (ThreadPoolUtil.class) {
				if (null == pool) {
					pool = Executors.newFixedThreadPool(1000, new DaemonThreadFactory());
				}
			}
		}
	}

	/**
	 * 执行线程<br>
	 * 执行线程，直到线程执行完成<br>
	 * 
	 * @param thread
	 */
	public static void execute(Runnable thread) {
		init();
		pool.execute(thread);
	}

	/**
	 * 关闭线程池
	 */
	public static void shutDown() {
		if (null != pool) {
			pool.shutdownNow();
		}
	}

	/**
	 * 线程池产生的线程为守护线程
	 * 
	 * @comment :
	 * @author: lkclkc88@autonavi.com
	 */
	private static class DaemonThreadFactory implements ThreadFactory {
		public Thread newThread(Runnable r) {
			Thread t = new Thread(r);
			t.setDaemon(true);
			return t;
		}
	}
}
